package com.gongqi.org.controller;

import com.gongqi.org.service.IDepartmentService;
import com.gongqi.org.domain.Department;
import com.gongqi.org.query.DepartmentQuery;
import com.gongqi.base.utils.R;
import com.gongqi.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    public IDepartmentService departmentService;


    /**
     * 保存和修改公用的
     * @param department  传递的实体
     * @return R转换结果
     */
    @PutMapping
    public R addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.add(department);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public R delete(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return R.me();
        } catch (Exception e) {
        e.printStackTrace();
            return R.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public R get(@PathVariable("id")Long id)
    {
        try {
            Department department = departmentService.getById(id);
            return R.me().setResult(department);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public R list(){

        try {
            List< Department> list = departmentService.getAll();
            return R.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public R json(@RequestBody DepartmentQuery query)
    {
        try {
            PageList<Department> pageList = departmentService.queryPage(query);
            return R.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }
}
